package com.example.jobs.task.impl;

import com.example.framework.redis.core.util.RedisUtils;
import com.example.jobs.dao.TaskCronMapper;
import com.example.jobs.task.Task;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class Task1 implements Task {

    @Resource
    private TaskCronMapper taskCronMapper;

    @Override
    public void run() {
        // Redis分布式锁执行
        RedisUtils.tryRunTask(getTaskName(), getTaskName(),
                taskCronMapper.getLockTimeByTaskName(getTaskName()),
                TimeUnit.SECONDS,
                () -> {
                    log.info("run task1");
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                });
    }

}
